Add boundary checks and add tests for SparseTensorProtoToDenseTensorProto#27323
Merged
yuslepukhin merged 6 commits intomainfrom Feb 13, 2026
Merged
Add boundary checks and add tests for SparseTensorProtoToDenseTensorProto#27323yuslepukhin merged 6 commits intomainfrom
yuslepukhin merged 6 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to harden SparseTensorProtoToDenseTensorProto in ONNX Runtime by adding validation around sparse indices/shape handling and extending index dtype support, along with new unit tests to cover the behavior.
Changes:
- Refactors
CopySparseDatato takedense_dimsanddense_elements, and adds bounds checks for rank-1 (flattened) and rank-2 (COO) indices. - Adds input validation for indices rank, values rank, and (new) dimension constraints.
- Adds new unit tests covering multiple index dtypes, raw vs typed index storage, and several invalid-input scenarios.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
onnxruntime/test/framework/tensorutils_test.cc |
Adds coverage for sparse-to-dense conversion across index dtypes and invalid shapes/out-of-bounds cases. |
onnxruntime/core/framework/tensorprotoutils.h |
Updates/extends the API documentation for sparse-to-dense conversion. |
onnxruntime/core/framework/tensorprotoutils.cc |
Implements the refactor and new validation/bounds checking in the sparse-to-dense conversion path. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
skottmckay
reviewed
Feb 12, 2026
tianleiwu
pushed a commit
that referenced
this pull request
Feb 13, 2026
…roto (#27323) ### Motivation and Context ### Description This pull request improves the robustness and correctness of sparse tensor conversion to dense tensors in the ONNX Runtime codebase. The main changes include enhanced validation of tensor shapes and indices, improved support for different index data types, and better error handling for out-of-bounds indices and invalid shapes. **Validation and Error Handling Improvements:** - Added explicit checks to ensure that sparse tensor indices are either rank 1 or 2, and that values are 1D, returning clear errors for unsupported shapes. Also, added checks to ensure all dimension sizes are positive for both values and dense tensors. - Improved error messages and validation for out-of-bounds indices during the sparse-to-dense copy process, including both flattened and multi-dimensional COO indices. [[1]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1827-R1850) [[2]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406R1861-R1863) **Index Data Type Handling:** - Enhanced support for INT8 and INT16 index types by allowing them to be provided in the `int32_data` field (not just as raw data), and improved handling of index data extraction for all supported types. **API and Internal Consistency:** - Refactored the `CopySparseData` function to take both `dense_dims` and `dense_elements` as arguments for clearer logic and consistency. [[1]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1747-R1781) [[2]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1904-R1947) [[3]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1928-R1957) [[4]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1938-R1967) - Updated function documentation to clarify supported formats and expected tensor shapes. **Minor Corrections:** - Improved error messages for invalid index shapes and ensured correct usage of ONNX enum values and utility functions. [[1]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1863-R1872) [[2]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1747-R1781) These changes collectively make sparse tensor handling more robust, user-friendly, and standards-compliant. ### Motivation and Context Make the code more robust
tianleiwu
added a commit
that referenced
this pull request
Feb 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Description
This pull request improves the robustness and correctness of sparse tensor conversion to dense tensors in the ONNX Runtime codebase. The main changes include enhanced validation of tensor shapes and indices, improved support for different index data types, and better error handling for out-of-bounds indices and invalid shapes.
Validation and Error Handling Improvements:
Index Data Type Handling:
int32_datafield (not just as raw data), and improved handling of index data extraction for all supported types.API and Internal Consistency:
CopySparseDatafunction to take bothdense_dimsanddense_elementsas arguments for clearer logic and consistency. [1] [2] [3] [4]Minor Corrections:
These changes collectively make sparse tensor handling more robust, user-friendly, and standards-compliant.
Motivation and Context
Make the code more robust